/*
 * @描述: 
 * @创建者: kilomChou
 * @Date: 2021-04-11 17:21:46
 * @最后编辑: kilomChou
 * @LastEditTime: 2021-05-20 16:55:56
 */
import Snake from "./Snake";
import { IAngCircle, IAngPos, Vec2 } from "../../core/vec2/Vec2";
import CircleEntity from "../entity/CircleEntity";
const [v2] = [Vec2];

/** 蛇节点类 */
export default class SnakeNode extends CircleEntity {

    /** 所属蛇 */
    snake: Snake
    /** 第几节点 */
    idx: number

    /** 初始化 */
    init(init: ISnakeNodeInit) {
        v2.angCirAs(this, init);
        this.snake = init.snake;
        this.idx = init.idx + 1;
    }

    /** 移动 */
    move() {
        let idx = this.idx;
        let moveDis = this.snake.saveDis;
        let op = this.op.angPosAs(this.snake.savePath[this.idx]);
        let nextPos: IAngPos;
        while (nextPos = this.snake.savePath[idx--]) {
            let dis = op.dis(nextPos);
            if (moveDis > dis) {
                moveDis -= dis;
                op.angPosAs(nextPos);
            } else break;
        }
        op.moveForward(moveDis);
        this.updateChunks();
    }

    /** 更新所覆盖的区块数组 */
    updateChunks() {
        this.world.snakeMgr.updateSnakeNodeChunks(this);
    }

}

/** 蛇节点初始化参数 */
export interface ISnakeNodeInit extends IAngCircle {
    /** 所属蛇 */
    snake: Snake
    /** 上个节点的idx */
    idx: number
}